/**
 * https://leetcode.cn/problems/find-k-closest-elements/description/
 * 658. 找到 K 个最接近的元素
 * medium, 赖德檀 2024.09.04
 * 滑动窗口
 */
class Solution {
public:
    vector<int> findClosestElements(vector<int>& arr, int k, int x) {
        vector<int>brr;
        int n=arr.size();
        int l=0,r=n-1;
        for(int i=0;i<n-k;i++)
        {
            if(abs(arr[l]-x)<=abs(arr[r]-x))
            r--;
            else 
            l++;
        }
        for(int i=l;i<=r;i++)
        brr.push_back(arr[i]);
        return brr;
    }
};